package com.faf.ui;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.bean.Comment;
import com.faf.bean.Food;
import com.faf.dao.CommentDao;
import com.faf.dao.FoodDao;
import com.faf.dao.impl.CommentDaoImpl;
import com.faf.dao.impl.FoodDaoImpl;
import com.faf.service.CommentService;
import com.faf.service.FoodService;
import com.faf.service.impl.CommentServiceImpl;
import com.faf.service.impl.FoodServiceImpl;
import com.faf.tools.Page;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class FoodPriceSort
 */
@WebServlet("/FoodPriceSort")
public class FoodPriceSort extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FoodPriceSort() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		int foodTypeId = Integer.parseInt(request.getParameter("foodType"));
		String foodType = null;
		if (foodTypeId == 1) {
			foodType = "炒菜";
		} else if (foodTypeId == 2) {
			foodType = "主食";
		} else if (foodTypeId == 3) {
			foodType = "凉菜";
		} else if (foodTypeId == 4) {
			foodType = "饮品";
		} else if (foodTypeId == 5) {
			foodType = "速食";
		} else {
			foodType = "凉菜";
		}
		FoodService foodService = new FoodServiceImpl();
		FoodDao foodDao = new FoodDaoImpl();
		try {
			// ��ȡ��ǰҳ��
			String currentPageStr = request.getParameter("currentPage");
			System.out.println(currentPageStr);
			if (currentPageStr == null) {
				currentPageStr = "1";
			}
			int pageTotal = 10; // ÿҳ��ʾ�ļ�¼��
			// String תint
			int current = Integer.parseInt(currentPageStr);
			// ��ȡ��ǰҳ������
			ArrayList<Food> foods = foodService.getFoodByFoodPrice(foodType, current, pageTotal);
			// ��ȡ�ܼ�¼��
			int total = foodDao.getAllFoodRecordByFoodType(foodType);
			// ����page����
			Page page = new Page(current, pageTotal);
			page.setResult(foods, total);
			// JSONArray jsonArray = JSONArray.fromObject(foods); // ��java����ת����ת����json����
			JSONObject dataPool = JSONObject.fromObject(page);
			dataPool.put("page", dataPool);
			response.getWriter().print(dataPool);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
